<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <div class="perform content-top">
        <div class="per-top">
          <div class="list-nav">本月班组生产指标</div>
          <div class="per-chart">
            <div class="chart-title">班组工时统计</div>
            <canvas id="perChart"></canvas>
          </div>
        </div>

        <div class="per-bottom">
          <div class="list-nav">个人生产指标</div>
          <van-button
            color="#02a7f0"
            @click="showPerson"
            type="primary"
            size="mini"
            style="margin-left: 5px"
            >个人
          </van-button>
          <van-button
            color="#02a7f0"
            @click="showTeam"
            type="primary"
            size="mini"
            >班组</van-button
          >
          <div class="content">
            <div class="per-chart" v-show="isPersonShow">
              <canvas class="chart-box" id="myChart"></canvas>
            </div>
            <table
              border="0"
              cellPadding="0"
              cellspacing="0"
              v-show="isTeamShow"
            >
              <thead>
                <tr>
                  <th colspan="5">工时</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>总产量</td>
                  <td>不良品</td>
                  <td>有效产量</td>
                  <td>生产工时</td>
                  <td>辅助工时</td>
                </tr>
                <tr>
                  <td>{{ personalTime.TOTLE || 0 }}</td>
                  <td>{{ personalTime.DEFECT || 0 }}</td>
                  <td>{{ personalTime.USEFUL || 0 }}</td>
                  <td>{{ personalTime.GS || 0 }}</td>
                  <td>{{ personalFuzhuTime.TOTLETIME || 0 }}</td>
                </tr>
              </tbody>
            </table>
          </div>

          <div class="assist">
            <div class="assist-box" v-on:click="toApplyList()">
              个人辅助工时申请
            </div>
            <div class="assist-box" v-on:click="toApplyDoneList()">
              辅助工时已申请列表
            </div>
            <div
              class="assist-box"
              v-on:click="toApplyCheckList()"
              v-if="user.usercode == 'A065-3336'"
            >
              辅助工时审核列表
            </div>
            <div
              class="assist-box"
              v-on:click="toApplyCheckDoneList()"
              v-if="user.usercode === 'A065-3336'"
            >
              辅助工时已审核列表
            </div>
          </div>
        </div>
      </div>
    </van-pull-refresh>
  </div>
</template>

<script>
import NavBar from "../../components/NavBar.vue";
import { Dialog } from "vant";
import {
  getPerfromInfo,
  getPerfromPaim,
  getPersonalTime,
  getFuzhuTime,
} from "../../api/perfrom";

// 引入 ECharts 主模块
var echarts = require("echarts");
export default {
  name: "perform",
  data() {
    return {
      perfromInfo: [],
      perfromPaim: [],
      personalTime: [],
      personalFuzhuTime: [],
      isPersonShow: true,
      isTeamShow: false,
      user: "",
      isLoading: false,
    };
  },
  components: {
    [Dialog.Component.name]: Dialog.Component,
    NavBar,
  },

  methods: {
    onRefresh() {
      this.getPerfromInfo();
      this.getPerfromPaim();
    },
    toApplyList() {
      if (this.user) {
        this.$router.push("/perform/applyList");
      } else {
        Dialog.confirm({
          message: "当前没有登录信息,请点击确认跳转登录",
          theme: "round-button",
          closeOnClickOverlay: true,
          confirmButtonColor: "#066ef4",
          cancelButtonColor: "#1890FF",
        })
          .then(() => {
            this.$router.push("/login");
          })
          .catch(() => {
            // on cancel
          });
      }
    },
    toApplyDoneList() {
      if (this.user) {
        this.$router.push("/perform/applyDoneList");
      } else {
        Dialog.confirm({
          message: "当前没有登录信息,请点击确认跳转登录",
          theme: "round-button",
          closeOnClickOverlay: true,
          confirmButtonColor: "#066ef4",
          cancelButtonColor: "#1890FF",
        })
          .then(() => {
            this.$router.push("/login");
          })
          .catch(() => {
            // on cancel
          });
      }
    },
    toApplyCheckList() {
      if (this.user) {
        this.$router.push("/perform/applyCheckList");
      } else {
        Dialog.confirm({
          message: "当前没有登录信息,请点击确认跳转登录",
          theme: "round-button",
          closeOnClickOverlay: true,
          confirmButtonColor: "#066ef4",
          cancelButtonColor: "#1890FF",
        })
          .then(() => {
            this.$router.push("/login");
          })
          .catch(() => {
            // on cancel
          });
      }
    },
    toApplyCheckDoneList() {
      if (this.user) {
        this.$router.push("/perform/applyCheckDoneList");
      } else {
        Dialog.confirm({
          message: "当前没有登录信息,请点击确认跳转登录",
          theme: "round-button",
          closeOnClickOverlay: true,
          confirmButtonColor: "#066ef4",
          cancelButtonColor: "#1890FF",
        })
          .then(() => {
            this.$router.push("/login");
          })
          .catch(() => {
            // on cancel
          });
      }
    },
    //班组工时信息
    getPerfromInfo() {
      this.$store.commit("showLoading");
      getPerfromInfo()
        .then((res) => {
          this.$store.commit("hideLoading");
          for (var i = 0; i < res.data.length; i++) {
            res.data[i].GSHJ = res.data[i].GSHJ.toFixed(1);
          }
          this.perfromInfo = res.data;
          this.isLoading = false;
          this.perChart();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //工时排名信息
    getPerfromPaim() {
      getPerfromPaim()
        .then((res) => {
          console.log(res);
          for (var i = 0; i < res.data.length; i++) {
            if(res.data[i].GS){
              res.data[i].GS = res.data[i].GS.toFixed(2);
            }else{
              res.data[i].GS = 0
            }
          }
          this.drawChart(res.data);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //个人信息
    getPersonalTime() {
      const user = window.localStorage.user;
      if (user) {
        // 个人工时信息
        getPersonalTime(user)
          .then((res) => {
            console.log(res);
            if (res.data.length == "0") {
              this.$toast("生产工时暂时没有数据");
            } else {
              this.personalTime = res.data[0];
            }
          })
          .catch((err) => {
            console.log(err);
          });
        // 个人辅助工时信息
        getFuzhuTime(user)
          .then((res) => {
            console.log(res);
            if (res.data.length == "0") {
              this.$toast("暂时没有辅助工时数据");
            } else {
              this.personalFuzhuTime = res.data[0];
            }
          })
          .catch((err) => {
            console.log(err);
          });

        this.isPersonShow = false;
        this.isTeamShow = true;
      } else {
        Dialog.confirm({
          message: "当前没有登录信息,请点击确认跳转登录",
          theme: "round-button",
          closeOnClickOverlay: true,
          confirmButtonColor: "#066ef4",
          cancelButtonColor: "#1890FF",
        })
          .then(() => {
            this.$router.push("/login");
          })
          .catch(() => {
            // on cancel
          });
      }
    },
    showPerson: function () {
      this.getPersonalTime();
    },
    showTeam: function () {
      this.isPersonShow = true;
      this.isTeamShow = false;
    },
    //班组工时统计表
    perChart() {
      var chartDom = document.getElementById("perChart");

      var myChart = echarts.getInstanceByDom(chartDom);
      if (myChart == null) {
        // 如果不存在，就进行初始化
        myChart = echarts.init(chartDom, null, {
          devicePixelRatio: 2.7,
        });
      }
      var option;
      var _this = this;
      option = {
        dataset: {
          dimensions: ["TIME", "GSHJ"],
          source: _this.perfromInfo,
        },
        grid: {
          left: "3%",
          right: "3%",
          bottom: "3%",
          top: 23,
          containLabel: true,
        },
        dataZoom: [
          {
            type: "inside",
            startValue: 0,
            endValue: 5,
            height: "30",
            bottom: "20",
            zoomLock: true,
          },
        ],
        xAxis: [
          {
            type: "category",
            axisTick: {
              alignWithLabel: true,
            },
            axisLabel: {
              fontSize: 10,
            },
          },
        ],
        yAxis: [
          {
            axisLabel: {
              fontSize: 10,
            },
          },
        ],
        series: [
          {
            type: "line",
            symbolSize: 1,
            // barWidth: "100%",
            label: {
              show: true,
              position: "top",
              color: "#555555",
              fontSize: 9,
            },
            lineStyle: {
              width: 1,
            },
            itemStyle: {
              color: "#066EF4",
            },
          },
        ],
      };

      option && myChart.setOption(option);
    },
    //精车班组个人工时排名表
    drawChart(data) {
      var myChart  = echarts.getInstanceByDom(
        document.getElementById("myChart")
      );
      if (myChart == null) {
        // 如果不存在，就进行初始化
        myChart = echarts.init(document.getElementById("myChart"), null, {
          devicePixelRatio: 2.7,
        });
      }
      var option = {
        dataset: {
          dimensions: ["NAME", "GS"],
          source: data,
        },
        title: {
          text: "班组昨日工时排名",
          left: "center",
          top: "top",
          textStyle: {
            fontSize: 10,
            fontWeight: "normal",
          },
          show: true,
        },
        dataZoom: [
          {
            type: "inside",
            startValue: 0,
            endValue: 12,
            height: "30",
            bottom: "20",
            zoomLock: true,
          },
        ],
        grid: {
          left: "3%",
          right: "3%",
          bottom: "3%",
          top: 20,
          containLabel: true,
        },
        xAxis: {
          type: "category",
          axisLabel: { interval: 0, rotate: 30, fontSize: 7 },
        },
        yAxis: {
          axisLabel: {
            fontSize: 7,
          },
        },
        series: {
          type: "bar",
          label: {
            show: true,
            position: "top",
            color: "#555555",
            fontSize: 7,
          },
          itemStyle: {
            color: "#066EF4",
          },
        },
      };

      option && myChart.setOption(option);
    },
  },
  mounted() {
    this.getPerfromInfo();
    this.getPerfromPaim();
    if (window.localStorage.user) {
      this.user = JSON.parse(window.localStorage.user);
    } else {
      this.user = "";
    }
  },
};
</script>

<style scoped lang="less">
table {
  margin: 54px 0;
  color: #555555;
  width: 100%;
  border-right: 1px solid #7f7f7f;
  border-bottom: 1px solid #7f7f7f;
}

table tr th {
  font-weight: 300;
  padding: 6px;
  font-size: 15px;
  border-left: 1px solid #7f7f7f;
  border-top: 1px solid #7f7f7f;
}

table tr td {
  padding: 5px;
  text-align: center;
  font-size: 14px;
  border-left: 1px solid #7f7f7f;
  border-top: 1px solid #7f7f7f;
}

#perChart,
#myChart {
  width: 100%;
}

.assist {
  // margin-top: 15px;
  display: flex;
  justify-content: space-around;

  .assist-box {
    border: 1px solid #02a7f0;
    font-size: 12px;
    padding: 3px 5px;
    text-align: center;
    width: 75px;
    background-color: #02a7f0;
    color: #fff;
    border-radius: 5px;
  }
}

.list-nav {
  font-size: 0.4rem;
  height: 1.22rem;
  line-height: 1.22rem;
  text-align: center;
  background-color: #066ef4;
  color: #fff;
}

.chart-title {
  margin-top: 20px;
  font-size: 0.4rem;
  text-align: center;
  color: #555555;
}

.perform {
  margin-bottom: 70px;
}
</style>